/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.djdb.shop.dao;

import be.djdb.shop.Refunds;

import java.util.Date;
import java.util.List;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import javax.validation.ConstraintViolationException;


public class RefundsDAO {
    private Date date;    
    private final EntityManager em;
    public RefundsDAO(EntityManager em) {
        this.em = em;
    }
    public List<Refunds> findAllRefunds() throws DAOException{
        try{
            List<Refunds> results = em.createNamedQuery("Refunds.findAll", Refunds.class).getResultList();
            return results;
        } catch (PersistenceException ex) {
            ERROR_Control(ex, "refunds.cannot.findALLrefunds.errormsg");
        }
        return null;
    }
    public Refunds findRefund(long orderid) throws DAOException{
        try{
            TypedQuery<Refunds> q = em.createNamedQuery("Refunds.findByOrderId",Refunds.class);
            q.setParameter("orderid", orderid);
            return q.getSingleResult();
        } catch (NoResultException ex) {
            Logger.getLogger(Refunds.class.getName()).log(Level.WARNING, ex.getMessage(), ex);
            return null;
        } catch (PersistenceException ex) {
            ERROR_Control(ex, "refunds.cannot.findProduct.errormsg");
        }
        return null;
    }
    public boolean isexist(String undefined) throws DAOException{
        try{
            TypedQuery<Long> q = em.createNamedQuery(".checkTitle", Long.class);
            q.setParameter("s",undefined);
            return q.getSingleResult() == 0;
        } catch (PersistenceException ex) {
            ERROR_Control(ex, "refunds.cannot.checkifexist.errormsg");
            throw new UnsupportedOperationException("Not supported yet.");
        }
        //return false;
    }
    public void addRefund(Refunds r) throws DAOException{
        try {
            em.getTransaction().begin();
            em.persist(r);        
            em.getTransaction().commit();
        } catch (ConstraintViolationException | PersistenceException ex) {
            String message = ex.getMessage();
            Throwable nestedException = ex;
            while (nestedException != null) {
                if (nestedException instanceof ConstraintViolationException) {
                    //message = Util.buildErrorMessage((ConstraintViolationException)nestedException);
                    break;
                }
                nestedException = nestedException.getCause();
            }
            ERROR_Control(ex, "refunds.cannot.add.errormsg");
        }
    }
    public void updateRefund(Refunds r) throws DAOException{
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public void removeRefund(long id) throws DAOException{
        try {
            em.getTransaction().begin();
            Refunds r = em.find(Refunds.class, id);
            if (r != null) {
                em.remove(r);
            }
            em.getTransaction().commit();
        } catch (PersistenceException ex) {
            ERROR_Control(ex, "refunds.cannot.remove.errormsg");
        } 
    }
    public void removeRefund(Refunds r) throws DAOException{
        removeRefund(r.getId());
    }
    public Refunds get(int id) throws DAOException{
        try{
            TypedQuery<Refunds> q = em.createNamedQuery("r.findById", Refunds.class);
            q.setParameter("id", id);
            return q.getSingleResult();
        } catch (NoResultException ex) {
            Logger.getLogger(Refunds.class.getName()).log(Level.WARNING, ex.getMessage(), ex);
            return null;
        } catch (PersistenceException ex) {
            ERROR_Control(ex, "refunds.cannot.findProduct.errormsg");
        }
        return null;
    }
    private void ERROR_Control(PersistenceException ex, String productcannotremoveerrormsg) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    private void ERROR_Control(java.lang.RuntimeException ex, String productcannotadderrormsg) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    } 

}
